﻿using UnityEngine;
using System.Collections;

public class AggroController : MonoBehaviour 
{
	[SerializeField]
	float speed = 1f;

	GameObject player;
	bool playerInAggroRange;
	PlayerHealth playerHealth;
	int triggers = 0;

	// Use this for initialization
	void Start () 
	{
		player = GameObject.Find ("Player");
		playerHealth = player.GetComponent<PlayerHealth>();
	}
	
	// Update is called once per frame
	void Update () 
	{
		if (triggers > 0)
		{
			MoveTowardsPlayer();
		}
		if (playerHealth.hp <= 0)
		{
			triggers = 0;
		}
	}
	
	void OnTriggerEnter2D(Collider2D other)
	{
		if(other.gameObject == player)
		{
			Debug.Log ("Aggro Trigger");
			triggers++;
		}
	}
	
	void OnTriggerExit2D(Collider2D other)
	{
		Debug.Log ("Trigger Exit");
		if(other.gameObject == player)
		{
			triggers--;
		}
	}
	
	void MoveTowardsPlayer()
	{
		Debug.Log("Should follow player now");
		Vector3 movement = (player.transform.position - transform.position) * speed * Time.deltaTime;
		rigidbody2D.MovePosition(transform.position + movement);
		
	}
}
